package com.riteny.oauth.config;

import com.riteny.oauth.core.tokenstore.CustomInMemoryTokenStore;
import com.riteny.oauth.core.tokenstore.CustomTokenStore;
import com.riteny.oauth.core.userauth.CustomUserAuthenticationService;
import com.riteny.oauth.core.userauth.CustomUserDetailsAuthenticationProvider;
import com.riteny.oauth.core.userauth.CustomUserDetailsService;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationProvider;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;

/**
 * @author Riteny
 * 2021/5/10  16:14
 */
@Configuration
public class CustomServiceConfiguration {

    @Bean
    public CustomTokenStore customInMemoryTokenStore(CustomUserAuthenticationService customUserAuthenticationService) {
        return new CustomInMemoryTokenStore(customUserAuthenticationService);
    }

    @Bean
    public AuthenticationProvider customAuthenticationProvider(CustomUserDetailsService customUserDetailsService) {
        CustomUserDetailsAuthenticationProvider tmacsUserDetailsAuthenticationProvider = new CustomUserDetailsAuthenticationProvider();
        tmacsUserDetailsAuthenticationProvider.setUserDetailsService(customUserDetailsService);
        tmacsUserDetailsAuthenticationProvider.setHideUserNotFoundExceptions(false);
        tmacsUserDetailsAuthenticationProvider.setPasswordEncoder(this.passwordEncoder());
        return tmacsUserDetailsAuthenticationProvider;
    }

    @Bean
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }

    @Bean
    public CustomUserDetailsService customUserDetailsService(CustomUserAuthenticationService customUserAuthenticationService) {
        return new CustomUserDetailsService(customUserAuthenticationService);
    }
}
